package uk.ac.bath.domains.prey;

import uk.ac.bath.Logger;
import uk.ac.bath.environment.cell.BasicPopulationEnvironment;
import uk.ac.bath.legacy.Space;
import uk.ac.bath.environment.Fitness;
import uk.ac.bath.environment.View;



public class PreyEnvironment extends BasicPopulationEnvironment implements PreyViewable {

    HunterCell hunter;
    PreyCell prey;
    CellFactory factory;
    Space space;

    public PreyEnvironment(PreySpace space, CellFactory factory) {
        this.space = space;
        tweaks.addAll(HunterCell.tweaks);
        tweaks.addAll(factory.getTweaks());
        this.factory = factory;
    }

    public Space getSpace() {
        return space;
    }

    @Override
    protected void tick() {

        if (hunter == null || hunter.getFitness() < 0) {
            //     ((PreySpace) space).remove(hunter);
            synchronized (muex) {
                if (hunter != null) {
                    population.remove(hunter);
                }
                MyPoint p = ((PreySpace) space).home;
                population.add(hunter = factory.createRandomHunterCell(p));
            }
        }


        if (prey == null || prey.getFitness() < 0) {
            //     ((PreySpace) space).remove(hunter);
            synchronized (muex) {
                if (prey != null) {
                    population.remove(prey);
                }
                MyPoint p = ((PreySpace) space).randomFreePoint();
                population.add(prey = factory.createRandomPreyCell(p));
            }
        }

        ((PreySpace) space).update(this);
        hunter.tick();
        prey.tick();
    }

    public HunterCell getHunter() {
        return hunter;
    }

    public PreyCell getPrey() {
        return prey;
    }

    public View createSimulationView() {
       return new PreyView(this);
    }

    @Override
    protected void init() {
       // No action needed 
    }

    @Override
    public void nextEvaluation(Fitness fitness) {
        
    }

    public void setLogger(Logger log) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
//
//    @Override
//    protected void setFitness(float fitness) {
//        throw new UnsupportedOperationException("Not supported yet.");
//    }
//
//    @Override
//    public void nextEvaluation() {
//        throw new UnsupportedOperationException("Not supported yet.");
//    }
}

